<template>
  <fix-area ref="fixArea">
    <div style="width: 100%; display: flex; justify-content: space-between; align-items: flex-start">
      <form-block ref="form" :model="form">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名(非必填)" :clearable="true" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查 询</el-button>
        </el-form-item>
      </form-block>
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="actorList">
        <table-column label="排序" width="120px" prop="sort" fixed="left" />
        <table-column label="演员姓名" width="220px" prop="name" fixed="left" />
        <table-column label="演员简介" width="220px" prop="introduce" />
        <table-column label="演员头像" width="120px" prop="head_priture" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.head_priture)">
              <cell-media :url="row.head_priture" />
            </div>
          </template>
        </table-column>
        <!--        <table-column label="演员详情图" width="220px" prop="pictures" :use-slot="true">-->
        <!--          <template #default="{row}">-->
        <!--            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.pictures)">-->
        <!--              <cell-media :url="row.pictures" />-->
        <!--            </div>-->
        <!--          </template>-->
        <!--        </table-column>-->
        <table-column label="粉丝群二维码" width="120px" prop="fans_group_qrcode" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.fans_group_qrcode)">
              <cell-media :url="row.fans_group_qrcode" />
            </div>
          </template>
        </table-column>
        <table-column label="粉丝数量" width="220px" prop="likes_number" />
        <table-column label="演员状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="评论数量" width="220px" prop="comment_number" />
        <table-column label="礼物数量" width="220px" prop="gift_number" />
        <table-column label="操作" width="320px" :use-slot="true" fixed="right">
          <template #default="{row}">
            <el-button size="mini" type="warning" @click="editActor(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteActor(row.id)">删除</el-button>
            <el-button size="mini" type="info" @click="toOpus(row.id)">作品</el-button>
            <el-button size="mini" type="primary" @click="toSchedules(row.id)">排期</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :visible="createVisible" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :visible="editVisible" :row="row" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>

<script>
import { actorList, deleteActor, actorDetails } from '@/api/actor'
import Create from '@/views/actor/actor/component/Create.vue'
import Edit from '@/views/actor/actor/component/Edit.vue'
export default {
  name: 'Actor',
  components: {
    Create,
    Edit
  },
  data() {
    return {
      form: {
        name: '',
        page: 1
      },
      actorList: [],
      total: 0,
      loading: false,
      createVisible: false,
      editVisible: false,
      row: null
    }
  },
  async mounted() {
    try {
      this.loading = true
      await this.getActorList(this.form)
      this.loading = false
    } catch (error) {
      this.$errorMessage()
      this.actorList = []
      this.loading = false
    }
  },
  methods: {
    toSchedules(id) {
      this.$router.push({ name: 'Schedules', query: { id: id.toString() }})
    },
    toOpus(id) {
      this.$router.push({ name: 'Opus', query: { id: id.toString() }})
    },
    async editActor(id) {
      try {
        this.loading = true
        const actorDetailsRes = await actorDetails({ id })
        this.row = actorDetailsRes.data
        this.loading = false
        this.editVisible = true
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async deleteActor(id) {
      try {
        await this.$warningConfirm('确定删除?')
        try {
          this.loading = true
          await deleteActor({ id })
          try {
            this.loading = true
            await this.getActorList(this.form)
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.actorList = []
            this.loading = false
          }
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        console.log('取消删除')
      }
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        await this.getActorList(this.form)
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.actorList = []
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        await this.getActorList(this.form)
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.actorList = []
        this.loading = false
      }
    },
    async getActorList(form) {
      const actorListRes = await actorList(form)
      this.actorList = actorListRes.data.data
      this.total = actorListRes.data.total
    },
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        await this.getActorList(this.form)
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.actorList = []
        this.loading = false
      }
    },
    async search() {
      try {
        this.loading = true
        await this.getActorList(this.form)
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.actorList = []
        this.loading = false
      }
    }
  }
}
</script>
